// common.js
// Author: ThomW

var benchmarkTimer = null;
var scriptStartTime = getTime();
function getTime() { benchmarkTimer = new Date(); return benchmarkTimer.getTime(); }


// This function injects a resource file from the extension into the <head> of a page
function injectFile(filename)
{
	// var startTime = getTime();  	

	// Create the script block setting src to injected js file 
	var s = document.createElement('script');
	s.setAttribute('src',chrome.extension.getURL(filename)); 
	s.setAttribute('type', 'text/javascript'); 
	
	// Append this script block to the header where it's executed
	var h = document.getElementsByTagName('head')[0];
	if (typeof(h) != 'undefined')
	{ 
		h.appendChild(s);
	}
	else
	{
		var b = document.getElementsByTagName('body')[0];
		if (typeof(b) != 'undefined')
		{
			b.appendChild(s);
		}
		else
		{
			alert('SCRIPT INJECTION FAILED!');
		}
		
	} 
	
	// Each script adds single digit milliseconds to the loading time of the page
	// console.log('injectFile(' + filename + '): ' + (getTime()-startTime) + 'ms'); 
}

if (typeof(GM_getValue) == 'undefined')
{
	// GM_get/setValue based on pup's cookie code again
	GM_getValue = function(name, defaultValue) 
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) 
		{
			begin = dc.indexOf(prefix);
			if (begin != 0) return defaultValue;
		}
		else
		{
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
		{
			end = dc.length;
		}
		return unescape(dc.substring(begin + prefix.length, end));
	};
}

// console.log('START INJECTION: ' + location.href); 

// This injects the common functions, such as the GM_ replacement functions and the Comment Settings modifications
injectFile('injected/common.js');

// This injects each script that the user has activated.
// The first time this is run after the extension is installed, all of the 
// scripts are injected (hence the true/true) regardless of defaults in 
// common.js since main.js doesn't have access to that information 
if (GM_getValue('confirm_unload', 'true') == 'true') { injectFile('injected/confirm_unload.js'); }
if (GM_getValue('collapse_post', 'true') == 'true') { injectFile('injected/collapse.js'); }
if (GM_getValue('category_banners', 'true') == 'true') { injectFile('injected/category_banners.js'); }
if (GM_getValue('mod_marker', 'true') == 'true') { injectFile('injected/modmarker.js'); }
if (GM_getValue('comment_tags', 'true') == 'true') { injectFile('injected/comment_tags.js'); }
if (GM_getValue('message_user', 'true') == 'true') { injectFile('injected/message_user.js'); }
if (GM_getValue('new_comments_marker', 'true') == 'true') { injectFile('injected/newcommentsmarker.js'); }
if (GM_getValue('post_preview', 'true') == 'true') { injectFile('injected/post_preview.js'); }
if (GM_getValue('embed_video', 'true') == 'true') { injectFile('injected/video.js'); }
if (GM_getValue('Lolscript', 'true') == 'true') { injectFile('injected/lol.js'); }
if (GM_getValue('sparkly_comics', 'true') == 'true') { injectFile('injected/sparkly_comics.js'); }
if (GM_getValue('shack_message_notify', 'true') == 'true') { injectFile('injected/shack_message_notify.js'); }
if (GM_getValue('shacktimezone', 'true') == 'true') { injectFile('injected/shacktimezone.js'); }
if (GM_getValue('work_shack', 'true') == 'true') { injectFile('injected/work_shack.js'); }
if (GM_getValue('gegtiks_dinosaur_comics', 'true') == 'true') { injectFile('injected/gegtiks_dinosaur_comics.js'); }
if (GM_getValue('shugashack', 'true') == 'true') { injectFile('injected/shugashack.js'); }
if (GM_getValue('friendfoe', 'true') == 'true') { injectFile('injected/friendfoe.js'); }